package controllers

import (
	"cms/common"
	"cms/libs"
	"cms/model"
	"github.com/kataras/iris"
	"github.com/kataras/iris/mvc"
	"github.com/kataras/iris/sessions"
	"strconv"
	"strings"
)

type NavController struct {
	Ctx     iris.Context
	Session *sessions.Session
	Nav     model.Nav
}

func (c *NavController) Get() mvc.View {
	page, err := strconv.Atoi(c.Ctx.URLParam("page"))
	if err != nil || page < 1 {
		page = 1
	}
	search := make(map[string]string)
	search["title"] = c.Ctx.URLParamDefault("title", "")
	search["category_id"] = c.Ctx.URLParamDefault("category_id", "")
	search["show_position"] = c.Ctx.URLParamDefault("show_position", "")
	search["re"] = c.Ctx.URLParamDefault("re", "")

	list, total, totalPages := c.Nav.List(page, search)
	Category := model.Category{}
	CateList := Category.List()
	model.ListTree = []model.Category{}
	CateList = Category.GetTree(CateList, 0, 0)
	Re := model.Recommend{}
	ReList := Re.ListAll()

	pages := make(map[string]interface{})
	pages["pageNum"] = totalPages
	pages["current"] = page
	pages["totalCount"] = total
	return mvc.View{
		Name:   "nav/list.html",
		Layout: "shared/layout.html",
		Data: iris.Map{
			"Title":         "内容列表",
			"ReList":        ReList,
			"list":          list,
			"CateList":      CateList,
			"title":         search["title"],
			"category_id":   search["category_id"],
			"show_position": search["show_position"],
			"re":            search["re"],
			"pages":         pages,
		},
	}
}

func (c *NavController) GetAdd() mvc.View {
	Category := model.Category{}
	list := Category.List()
	model.ListTree = []model.Category{}
	list = Category.GetTree(list, 0, 0)
	Re := model.Recommend{}
	ReList := Re.ListAll()
	return mvc.View{
		Name:   "nav/add.html",
		Layout: "shared/layout.html",
		Data: iris.Map{
			"Title":  "新增内容",
			"list":   list,
			"ReList": ReList,
		},
	}
}

func (c *NavController) PostAdd() {
	_, _, err := c.Ctx.FormFile("image")
	filePath := ""
	if err == nil {
		err, filePath := libs.UploadFile("image", "indexImages", c.Ctx)
		if err == false {
			commons.DefaultErrorShow(filePath, c.Ctx)
			return
		}
	}
	if err := c.Nav.Add(c.Ctx.FormValues(), filePath); err == nil {
		c.Ctx.Redirect("/nav")
	} else {
		commons.DefaultErrorShow(err.Error(), c.Ctx)
	}
}

func (c *NavController) GetUpdateBy(id uint) mvc.View {
	NavInfo, err := c.Nav.Info(id)
	if err != nil {
		return commons.MvcError(err.Error(), c.Ctx)
	}
	Category := model.Category{}
	list := Category.List()
	model.ListTree = []model.Category{}
	list = Category.GetTree(list, 0, 0)
	Re := model.Recommend{}
	ReList := Re.ListAll()

	Recommend := []int{}
	for _, v := range strings.Split(NavInfo.Recommend, ",") {
		_v, _ := strconv.Atoi(v)
		Recommend = append(Recommend, _v)
	}

	return mvc.View{
		Name:   "nav/update.html",
		Layout: "shared/layout.html",
		Data: iris.Map{
			"Title":      "内容修改",
			"UpdateInfo": NavInfo,
			"list":       list,
			"ReList":     ReList,
			"Recommend":  Recommend,
		},
	}
}

func (c *NavController) PostUpdate() {
	_, _, err := c.Ctx.FormFile("image")
	filePath, rs := "", false
	if err == nil {
		rs, filePath = libs.UploadFile("image", "indexImages", c.Ctx)
		if rs == false {
			commons.DefaultErrorShow(filePath, c.Ctx)
			return
		}
	}

	if err := c.Nav.Update(c.Ctx.FormValues(), filePath); err == nil {
		c.Ctx.Redirect("/nav")
	} else {
		commons.DefaultErrorShow(err.Error(), c.Ctx)
	}
}

func (c *NavController) GetDelBy(id uint) {
	if err := c.Nav.Del(id); err == nil {
		c.Ctx.Redirect("/nav")
	} else {
		commons.DefaultErrorShow(err.Error(), c.Ctx)
	}
}

func (c *NavController) PostDelAll() string {
	ids := c.Ctx.PostValueDefault("ids", "")
	if ids == "" {
		return "false"
	}
	if err := c.Nav.DelAll(ids); err == nil {
		return "true"
	} else {
		return "false"
	}
}
